<template>
	<view>
		<scroll-view scroll-y id="scview" :scroll-top="scrollTop" :scroll-with-animation="true"
		 :style="{height:style.contentH+'px'}">
			
			<!-- 聊天列表 -->
			<block v-for="(item,index) in list" :key="index">
				<user-char-list :item="item" :index="index"></user-char-list>
			</block>
		</scroll-view>
			
			
			<!-- 输入框 -->
			<user-chat-bottom @submit="submit"></user-chat-bottom>
		</view>
</template>

<script>
	import userChatBottom from "../../components/user-chat/user-chat.vue";
	import time from "../../common/time.js";
	import userCharList from "../../components/user-chat/user-chat-list.vue"
	export default {
		
		components:{
			userChatBottom,
			userCharList
		},
		data() {
			return {
				scrollTop:0,
				itemH:"",
				style:{
					contentH:""
				},
				text:"",
				list:[
					{
						isme:false,
						userpic:"../../static/srcc/touxiang.jpg",
						type:"text",
						data:"haha ",
						time:"1554970014"
					},
					{
						isme:true,
						userpic:"../../static/srcc/touxiang.jpg",
						type:"img",
						data:"../../static/srcc/1.jpg",
						time:"1554970014"
					}
				]
			}
		},
		onLoad() {
			this.getdata();
			this.initdata()
		},
		onReady() {
			this.pagebottem()
		},
		methods: {
			//初始化参数
		initdata(){
			try{
				const res = uni.getSystemInfoSync();
				this.style.contentH = res.windowHeight-uni.upx2px(120);
			}catch(e){
				//TODO handle the exception
			}
		},
		pagebottem(){
			let q = uni.createSelectorQuery()
			q.select('#scview').boundingClientRect()
			q.selectAll('.user-chat-item').boundingClientRect()
			
			q.exec((res)=>{
				res[1].forEach((ret)=>{
					this.style.itemH += ret.height
				})
			})
		},
			getdata(){
				// 获取聊天数据
				let arr=[
						{
							isme:false,
							userpic:"../../static/srcc/touxiang.jpg",
							type:"text",
							data:"haha ",
							time:"1610372465",
							gstime:""
						},
						{
							isme:true,
							userpic:"../../static/srcc/2.jpg",
							type:"img",
							data:"../../static/srcc/1.jpg",
							time:"1610372769",
							gstime:""
						}
					
				];
				for(let i =0;i<arr.length;i++){
					arr[i].gstime=time.gettime.getChatTime(arr[i].time,i>0?arr[i-1].time:0)
				}
				this.list = arr;
			},
			submit(data){
							// 发送逻辑
							console.log("当前输入的是"+data)
							let now = new Date().getTime()
							let obj = {
								isme:true,
								userpic:"../../static/srcc/2.jpg",
								type:"text",
								data:data,
								time:"1610372769",
								gstime:time.gettime.getChatTime(now,this.list[this.list.length-1].time)
							}
							this.list.push(obj)
						}
		}
	}
</script>

<style>

</style>
